package com.aeon.main;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.lang.management.ManagementFactory;

import javax.swing.JButton;
import javax.swing.JFrame;

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;

/**
 * Profiler
 * 
 * @version 1.0
 * @since 1.0
 * */
public class Main {

	public static void main(String[] args) {
		m1(args);
	}

	private static void m1(String[] args) {
		String pid = "4256";
		if (args.length != 0) {
			pid = args[0];
		}
		System.out.println("asdc");
		JFrame frame = new JFrame();
		frame.setSize(200, 300);
		frame.add(getButton(pid));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	private static Component getButton(final String arg) {
		JButton button = new JButton("attach");
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				try {
					String pid = arg;
					if (pid == null) {
						String vmName = ManagementFactory.getRuntimeMXBean()
								.getName();
						int i = vmName.indexOf('@');
						pid = vmName.substring(0, i);
					}

					VirtualMachine vm = VirtualMachine.attach(pid);

					String s = "L:/swt/profilerTst/lib/AeonJPT.jar";
					vm.loadAgent(s, "eclipse");
					// vm.loadAgentPath("E:\\funPlace\\profiler\\Tst_AeonJTP\\lib\\AeonJTP1.jar");

					vm.detach();

					System.out.println("f");
				} catch (AttachNotSupportedException | IOException
						| AgentLoadException | AgentInitializationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		return button;
	}
}
